import java.util.Arrays;
public class hk6Q2
{
    public static void main(String[] args) 
    {
    //实验环境测试版
    int[] position = {0,1,2,3,4,5,6,7,8,9,10,11};
    System.out.println("The number's position is " + Arrays.toString(position) + "\t");
    int[] chase = {4,5,5,6,7,7,8,9,58,110,233,666};
    //如果我一开始输入的是一个乱序的数组，在排序后它的顺序肯定就变了，此时输出的位置还是我想要找的位置吗？
   /* int low = 0;
    int high = chase.length;
    //int index = Arrays.binarySearch(chase, 233);
    int index = -1;*/
    //Arrays.sort(chase);
    System.out.println("-----------------------------------------------------------------------------");
    System.out.println("The ordered array is " + Arrays.toString(chase) + "\t");
    int low = 0;
    int high = chase.length - 1;
    //int index = Arrays.binarySearch(chase, 233);
    //int find = 233;
    int mid = 0;
    int index = 0;
    while (low <= high)
    {
        mid = (low + high) / 2;
        //int guess = chase[mid];
        if(chase[mid] == 233)
        {
            index = mid;
            //System.out.println("The number is 233， and its house lie in" + index);
            break;
        }
        if(chase[mid] < 233)
        {
            low = mid + 1;
        }
        if(chase[mid] > 233)
        {
            high = mid - 1;
        }
    //break;
    //System.out.println("The number is 233， and its house lie in" + Arrays.binarySearch(chase, 233));
    }
    System.out.println("-----------------------------------------------------------------------------");
    //System.out.println("The number we're chase for is 233" + "and its house lies in " + index);
    //if (index != -1)
    if (chase[mid] == 233)
    {
        System.out.println("The number we're chase for is 233, " + "and its house lies in " + index);
    }
    else
    {
        System.out.println("The number is not found!");
    }
    //System.out.println("The number is not found!");
    //System.out.println("The number is 233， and its house lie in" + index);
    }
    //我先输入一个数组，最后要返回和输入的是索引号index
    //要用二分法，首先得设定一个高位high和一个低位low，当然还有一个中位mid
    //当arr[mid]比我们要找的数字大或小或等时，都要用一个if来判断
    //假如大了，令低位为中位加一；假如小了，令高位为中位减一
    //找到了就直接break，找不到就只能找到了直接返回。
}